import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: YKH
 * Date: 2022-05-07
 * Time: 22:03
 * author:Listen
 */
public class Listen {
    public static void main(String[] args) {
        Frame frame = new Frame("开始 - 结束");
        Button button1 = new Button("start");
        Button button2 = new Button("exit");
        button1.setActionCommand("start-1");
        button2.setActionCommand("stop - 1");
        button1.addActionListener(new MyActionListener());
        button2.addActionListener(new MyActionListener());
        frame.setLayout(new GridLayout(2,1));
        frame.add(button1);
        frame.add(button2);
        frame.setVisible(true);
        windowClose(frame);


    }
    //关闭窗口方法
    private static void windowClose(Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

class MyActionListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        // e.getActionCommand() 获得按键的信息
        System.out.println("按钮被点击的了" + e.getActionCommand());
    }
}
